package com.card.admin.pojo;

import java.util.List;

import com.card.user.StringParam;
import com.google.common.collect.Lists;
import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.FilterOperator;
import com.xiaoao.net.db.Key;
import com.xiaoao.net.db.Query;
import com.xiaoao.net.db.Query.FilterPredicate;
import com.xiaoao.net.db.mongo.MongodbManager;
import com.xiaoao.net.utils.PrimaryKey;
import com.xiaoao.net.utils.StringUtil;

/**
 * @Description:
 * @author siyunlong
 * @date 2016年8月15日 下午7:00:46
 * @version V1.0
 */
public class AdminManager {
	public static final String T_Admin_DATA = "admin";
	public static final String T_User_DATA = "userdata";
	private static final AdminManager instance = new AdminManager();

	public static AdminManager getInstance() {
		return instance;
	}

	public void init() {
		Admin admin = getAdmin("admin");
		if (admin == null) {
			addAdmin("admin", "123");
		}
	}

	public List<Admin> getAgents(Admin admin) {
		if (admin.getGroup() == StringParam.Group_Admin) {
			return getAdminByGroup(StringParam.Group_Agent);
		}
		return getAdminByFatherId(admin.getId());
	}

	public List<Admin> getAdminByFatherId(int fatherId) {
		Query query = new Query(T_Admin_DATA);
		query.addFilter(new FilterPredicate(StringParam.FatherId, FilterOperator.EQUAL, fatherId));
		query.setLimit(10000);
		List<Entity> entityList = DBManager.getInstance().queryEntity(query);
		List<Admin> ads = Lists.newArrayList();
		for (Entity entity : entityList) {
			ads.add(new Admin(entity));
		}
		return ads;
	}

	public static Admin getAdmin(String userName) {
		// 从数据库获取玩家数据
		List<Entity> entityList = getEntity(userName);
		if (entityList.size() > 0) {
			Entity entity = entityList.get(0);
			return new Admin(entity);
		}
		return null;
	}

	public Admin getAdminById(String id) {
		// 从数据库获取玩家数据
		Entity entity = MongodbManager.getInstance().getEntityFromDB(new Key(T_Admin_DATA, id));
		if (entity != null) {
			return new Admin(entity);
		}
		return null;
	}

	public List<Admin> getAdminByGroup(int group) {
		List<Admin> ads = Lists.newArrayList();
		List<Entity> entityList = getEntityByGroup(group);
		for (Entity entity : entityList) {
			ads.add(new Admin(entity));
		}
		return ads;
	}

	public List<Admin> getAgents() {
		return getAdminByGroup(StringParam.Group_Agent);
	}

	private static List<Entity> getEntity(String userName) {
		Query query = new Query(T_Admin_DATA);
		query.addFilter(new FilterPredicate(StringParam.UserName, FilterOperator.EQUAL, userName));
		query.setLimit(1);
		List<Entity> entityList = DBManager.getInstance().queryEntity(query);
		return entityList;
	}

	private List<Entity> getEntityByGroup(int group) {
		Query query = new Query(T_Admin_DATA);
		query.addFilter(new FilterPredicate(StringParam.Group, FilterOperator.EQUAL, group));
		query.setLimit(10000);
		List<Entity> entityList = DBManager.getInstance().queryEntity(query);
		return entityList;
	}

	public boolean addAdmin(String userName, String pwd) {
		List<Entity> entityList = getEntity(userName);
		if (!entityList.isEmpty()) {
			return false;
		}
		Entity entity = new Entity(T_Admin_DATA, PrimaryKey.getIntPrimaryKey("admin") + "");
		entity.setProperty(StringParam.UserName, userName);
		entity.setProperty(StringParam.Pwd, pwd);
		entity.setProperty(StringParam.Group, StringParam.Group_Admin);
		DBManager.getInstance().saveOrUpdate(entity);
		return true;
	}

	public boolean addAgent(int fatherId, String userName, String pwd, String phone) {
		List<Entity> entityList = getEntity(userName);
		if (!entityList.isEmpty()) {
			return false;
		}
		// 添加新代理
		Entity entity = new Entity(T_Admin_DATA, PrimaryKey.getIntPrimaryKey("admin") + "");
		entity.setProperty(StringParam.UserName, userName);
		entity.setProperty(StringParam.Pwd, pwd);
		entity.setProperty(StringParam.Phone, phone);
		entity.setProperty(StringParam.FatherId, fatherId);
		entity.setProperty(StringParam.Group, StringParam.Group_Agent);
		//entity.setProperty(StringParam.RoomSettings, "4,4,1,0,1,6,gonghui");
		DBManager.getInstance().saveOrUpdate(entity);

		// 更新父代理
		// Admin admin = AdminManager.getInstance().getAdmin(userName);
		// System.out.println("更新父代理" + admin.getFatherId());
		//
		// Entity entity1 = new Entity(T_User_DATA,
		// PrimaryKey.getIntPrimaryKey("userdata") + "");
		// entity.setProperty(StringParam.ID, userName);
		// entity.setProperty(StringParam.FatherId, admin.getFatherId());
		// DBManager.getInstance().saveOrUpdate(entity1);
		return true;
	}

	public void changeState(String id) {
		Admin admin = getAdminById(id);
		if (admin == null) {
			return;
		}
		Entity entity = admin.getEntity();
		int state = admin.getState();
		state = state == StringParam.State_Free ? StringParam.State_Lock : StringParam.State_Free;
		entity.setProperty(StringParam.State, state);
		DBManager.getInstance().saveOrUpdate(entity);
	}

	public void deleteAgent(String id) {
		Admin admin = getAdminById(id);
		if (admin == null) {
			return;
		}
		DBManager.getInstance().removeEntity(new Key(T_Admin_DATA, id));
	}

	public void changePwd(String id, String newPwd) {
		Admin admin = getAdminById(id);
		if (admin == null) {
			return;
		}
		Entity entity = admin.getEntity();
		entity.setProperty(StringParam.Pwd, newPwd);
		DBManager.getInstance().saveOrUpdate(entity);
	}

	public void addGold(String id, int addGold, int toId) {
		Admin admin = getAdminById(id);
		if (admin == null) {
			return;
		}
		Entity entity = admin.getEntity();
		int gold = admin.getGold() + addGold;
		admin.setGold(gold);
		entity.setProperty(StringParam.Gold, gold);
		admin.addCzLog(addGold, gold, toId);
		admin.saveDB();
	}

	/*// 更改gpgp
	public void changRoomSetting(String id, String roonsetting) {
		Admin admin = getAdminById(id);
		if (admin == null) {
			return;
		}
		Entity entity = admin.getEntity();
		admin.setRoomSettings(roonsetting);
		entity.setProperty(StringParam.RoomSettings, roonsetting);
		admin.saveDB();
	}*/

	public void addGold(Admin admin, int addGold, String id) {
		Entity entity = admin.getEntity();
		int gold = admin.getGold() + addGold;
		admin.setGold(gold);
		entity.setProperty(StringParam.Gold, gold);
		admin.addCzLog(addGold, gold, Integer.parseInt(id));
		admin.saveDB();
	}

	public int resetPwd(String userName, String pwd, String newPwd) {
		Admin admin = getAdmin(userName);
		if (admin == null) {
			return 1;
		}
		if (!StringUtil.equals(admin.getPassword(), pwd)) {
			return 2;
		}
		Entity entity = admin.getEntity();
		entity.setProperty(StringParam.Pwd, newPwd);
		DBManager.getInstance().saveOrUpdate(entity);
		return 0;
	}

	public static void main(String[] args) {
		AdminManager manager = AdminManager.getInstance();
	}
}
